package com.myk.game.heroscuffle.heroes;

import androidx.annotation.NonNull;

import com.myk.game.heroscuffle.game.Attacked;
import com.myk.game.heroscuffle.game.Element;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Sign;
import com.myk.game.heroscuffle.game.Skill;
import com.myk.game.heroscuffle.utils.RdmPropUtil;
import com.myk.game.heroscuffle.utils.SC;
import com.myk.game.heroscuffle.utils.WeighterTask;

public class Hero_FengLing extends Hero {

    public Hero_FengLing() {
        super("风凌", "每回合开始自动损失一定生命值，他非常任性几乎不服从玩家的指定。" +
                        SC.TLI + "攻击后不会被敌人记住，还会给予目标随机增益。被攻击后也不记住敌人，且有几率避免这次伤害。" +
                        SC.TLI + "此外他还不能被附加任何标记。",
                Element.WIND, 1200, 150, 50);
    }

    @Override
    public void setEnemy(@NonNull Hero enemy) {
        //不记住任何敌人
    }

    @Override
    public void setPrey(@NonNull Hero prey) {
        //不记住任何猎物
    }

    @Override
    public void pasteSign(Sign sign) {
        //不能被添加标记
    }

    @Override
    public void onPreAction() {
        game.ui.printlnMsg(getObviousName() + "独自哀咽，");
        sufferDamage(null, null, null, 15);
    }

    @Override
    public void onAcceptPlayerAttackAction(Attacked preAttacked) {
        if (game.pool.getAliveCount() > 3) { //有两个以上敌人时，完全不服从玩家指定
            Hero anther = game.pool.getAnyoneAliveHero(this, preAttacked.getTarget());
            this.doAttackActionStart(new Attacked(anther, "任性自由去攻击"));
        }
        else if (game.pool.getAliveCount() == 3) { //只有两个敌人时，也不会让玩家反向选定的
            if (RdmPropUtil.bingo(1 / 2d)) {
                Hero anther = game.pool.getAnyoneAliveHero(this, preAttacked.getTarget());
                this.doAttackActionStart(new Attacked(anther, "犹犹豫豫去攻击"));
            }
            else {
                preAttacked.setReason("犹犹豫豫去攻击");
                this.doAttackActionStart(preAttacked);
            }
        }
        else { //只有一个敌人了，没办法才听玩家的
            preAttacked.setReason("无可奈何去去攻击");
            this.doAttackActionStart(preAttacked);
        }
    }

    @Override
    public void onAfterDoAttack(Hero attacked, int damage, boolean isToDeath) {
        if (attacked.state.recentEnemy == this) {
            attacked.clearEnemy();
        }
        if (!isToDeath) {
            game.ui.sleepShortly();
            game.ui.printMsg("风去留情," + attacked.getObviousName());
            RdmPropUtil.lotteryExecute(
                    new WeighterTask(1, () -> attacked.addHpWithPrint(20)),
                    new WeighterTask(1, () -> attacked.addAtkWithPrint(5)),
                    new WeighterTask(1, () -> attacked.addDefWithPrint(2)),
                    new WeighterTask(1, () -> {
                        //从前往后搜索未准备好的技能
                        Skill luckSkill = null;
                        for (Skill skill : attacked.SKILLS) {
                            if (!skill.isReady()) {
                                luckSkill = skill;
                                break;
                            }
                        }
                        if (luckSkill != null) {//激活技能
                            if (luckSkill.remnantCoolingTime >= 0) {
                                luckSkill.remnantCoolingTime = -1;
                            }
                            if (luckSkill.remnantUsableTimes < 1) {
                                luckSkill.remnantUsableTimes = 1;
                            }
                            game.ui.printMsg("激活技能" + luckSkill.SKILL_NAME + "。");
                        }
                        else {
                            attacked.addHpWithPrint(10);
                        }
                    })
            );
        }
    }

    @Override
    public void onAfterBeAttack(Hero attacker, int damage) {
        Hero originPrey = attacker.state.getAlivePrey(); //攻击者记住的原来的猎物
        attacker.event.onAfterDoAttack.addRunBack(eventData -> { //让攻击者攻击完成后仍然记住原来的猎物
            if (originPrey != null) {
                attacker.setPrey(originPrey);
            }
            else {
                attacker.clearPrey();
            }
            eventData.removeRunBackFromListener();
        });
    }

    @Override
    public void onAfterSufferDamage(Hero attacker, Skill sourceSkill, Sign sourceSign, int damage) {
        //被普通攻击时，有1/3的几率恢复受到的伤害
        if (attacker != null) {
            if (RdmPropUtil.bingo(1, 3)) {
                hp += damage;
                game.ui.printMsg(getObviousName() + "清风回旋,避免了这次伤害，");
                game.ui.sleepShortly();
                attacker.clearActioned();
                game.ui.printMsg(attacker.getObviousName() + "再获得一次行动机会，");
                game.ui.sleepShortly();
                game.ui.printMsg(getObviousName());
            }
        }
    }
}
